<?php

namespace Admin\Controller;

use function PHPSTORM_META\elementType;
use Think\Controller;

class StatisticsController extends CommenController
{
    /***
     * 日统计
     */
    public function day_statics()
    {

        $shop_identifier = $_SESSION['shop_identifier'];
        $choose_time = !empty(I('get.time')) ? strtotime(I('get.time')) : time();
        ///	1、每日放款统计  2、每日收款统 3、每日注册率统计4、每日通过率统计	5、数据汇总统计
        $time_s = date('Ymd', $choose_time);
        $redis = MycommonController::mk_redis();

        if (empty($redis->get($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_BORROW))) {
            for ($i = 7; $i >= 0; $i--) {
                //当天
                if ($i == 0) {
                    $date[] = date('Y-m-d', $choose_time);
                    $time = array(strtotime(date('Y-m-d', $choose_time)), strtotime(date('Y-m-d 23:59:59', $choose_time)));
                } else {
                    $date[] = date('Y-m-d', strtotime("-{$i} day", $choose_time));
                    $time = array(strtotime(date('Y-m-d', strtotime("-{$i} day", $choose_time))), strtotime(date('Y-m-d 23:59:59', strtotime("-{$i} day", $choose_time)))); //前一天
                }
                //每日放款统计
                $borrow_where['get_time'] = array('between', $time);
                $borrow_where['is_success'] = array('eq', 2);
                $borrow_where['shop_identifier'] = $shop_identifier;
                $borrow_money = M('vz_loan', '', 'XYC')->where($borrow_where)->sum('loan_amount');

                $borrow_arr[] = !empty($borrow_money) ? $borrow_money : 0;

                //每日收款统计
//                $repay_where['actual_time'] = array('between', $time);
////                $repay_where['is_success'] = array('eq', 2);
//                $repay_where['is_success'] = array('eq', 2);
//                $repay_where['shop_identifier'] = $shop_identifier;
//                $repay_money = M('vz_loan', '', 'XYC')->where($borrow_where)->sum('repayment_amount');
//                $repay_arr[] = !empty($repay_money) ? $repay_money : 0;

                //线下收款

                $repay_where['ctime']=array('between', $time);
                $repay_where['shop_identifier'] = $shop_identifier;
                $money_un_line=M('vz_loan_collection','','XYC')->where($repay_where)->sum('money');//收款额
                $money_un_line=!empty($money_un_line) ? $money_un_line : 0;
                $overdue_money=M('vz_loan_collection','','XYC')->where($repay_where)->sum('overdue_money');//滞纳金
                $overdue_money=!empty($overdue_money) ? $overdue_money : 0;

                //自主还款
                $app_money=M('vz_loan','','XYC')->join('a left join vz_loan_collection b on b.order_number=a.order_number')->where(['a.shop_identifier'=>$shop_identifier,'b.order_number'=>['exp','is null'],'actual_time'=>['between', $time]])->sum('a.loan_amount');
                $app_money=!empty($app_money) ? $app_money : 0;
                $repay_arr[]=$overdue_money+$money_un_line+$app_money;


                //每日注册率统计
                $register_arr[] = 0;


                //每日通过率统计
                $pass_where['cert_time'] = array('between', $time);
                $pass_where['shop_identifier'] = $shop_identifier;
                $cert_total = M('user', '', 'XYC')->where($pass_where)->count();
                $pass_where['cert_status'] = array('eq', 2);
                $pass_total = M('user', '', 'XYC')->where($pass_where)->count();
                if ($cert_total == '0') {
                    $pass_arr[] = 0;
                } else {
                    $pass_arr[] = intval($pass_total / $cert_total,2)*100;
                }
                $redis->setex($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_BORROW, 3600, json_encode($borrow_arr));
                $redis->setex($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_REPAY, 3600, json_encode($repay_arr));
                $redis->setex($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_REGISTER, 3600, json_encode($register_arr));
                $redis->setex($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_PASS, 3600, json_encode($pass_arr));
                $redis->setex($time_s . $shop_identifier . RedisPrefixController::VZ_DATE, 3600, json_encode($date));
            }
        } else {
            $borrow_arr = json_decode($redis->get($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_BORROW));
            $repay_arr = json_decode($redis->get($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_REPAY));
            $register_arr = json_decode($redis->get($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_REGISTER));
            $pass_arr = json_decode($redis->get($time_s . $shop_identifier . RedisPrefixController::VZ_DAY_PASS));
            $date = json_decode($redis->get($time_s . $shop_identifier . RedisPrefixController::VZ_DATE));
        }


        $this->assign(array(
            'date'=>json_encode($date),
            'borrow_arr'=>json_encode($borrow_arr),
            'repay_arr'=>json_encode($repay_arr),
            'register_arr'=>json_encode($register_arr),
            'pass_arr'=>json_encode($pass_arr),
        ));
        $this->display();

    }


}